package tcp

import (
	"gitee.com/injoyai/goutil/net/conn/v2"
)

// Client tcp客户端
type Client struct {
	*conn.Conn
}

// Redial 重试连接,递归重试
func (this *Client) Redial(fn ...func(c *Client)) *Client {
	var fns []func(c *conn.Conn)
	for _, v := range fn {
		fns = append(fns, func(c *conn.Conn) { v(&Client{c}) })
	}
	this.Conn.Redial(fns...)
	return this
}

// Dial 建立tcp连接
func Dial(addr string) (*Client, error) {
	c, err := conn.DialTCP(addr)
	data := &Client{Conn: c}
	return data, err
}

// Redial 重试连接
func Redial(addr string, fn ...func(c *Client)) *Client {
	return MustDial(addr).Redial(fn...)
}

// MustDial 一直连到成功
func MustDial(addr string) *Client {
	return &Client{conn.MustDialTCP(addr)}
}
